<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="600px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="130px" style="padding-right:30px">
      <el-form-item label="员工姓名" prop="username">
        <el-input v-model="dataForm.username" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="工号" prop="number">
        <el-input v-model="dataForm.number" placeholder="请输入工号"></el-input>
      </el-form-item>
      <el-form-item label="身份证号后6位" prop="idNumber">
        <el-input v-model="dataForm.idNumber" placeholder="请输入身份证号后6位" maxlength="6"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="telnum">
        <el-input v-model="dataForm.telnum" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="职工生日" prop="birthday">
        <el-date-picker
          @change="onBirthdayChange($event)"
          v-model="dataForm.birthday"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="dataForm.sex" clearable placeholder="请选择" style="width: 100%">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色" size="mini" prop="roleId">
          <el-radio v-model="dataForm.roleId" v-for="role in roleList"  :key="role.roleId" :label="role.roleId">{{ role.roleName }}</el-radio>
        <!-- <el-checkbox-group v-model="dataForm.roleId">
          <el-checkbox v-for="role in roleList" :key="role.roleId" :label="role.roleId">{{ role.roleName }}</el-checkbox>
        </el-checkbox-group> -->
      </el-form-item>
    </el-form>
    <br />
    <br />
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        username: '',
        number:'',
        idNumber:'',
        telnum:'',
        birthday:'',
        roleId:3,
        age: 0,
        sex:''
      },
      dataRule: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        number: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        idNumber: [{ required: true, message: '身份证号不能为空', trigger: 'blur' }],
        telnum: [{ required: true, message: '手机号不能为空', trigger: 'blur' }],
        birthday: [{ required: true, message: '生日不能为空', trigger: 'blur' }],
        age: [{ required: true, message: '年龄不能为空', trigger: 'blur' }],
        sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
      },
      typeList: [],
      loading:false,
      roleList: [{
        "roleId":1,"roleName":"管理员"
      },{
        "roleId":2,"roleName":"发餐人"
      },{
        "roleId":3,"roleName":"员工"
      }],
       options: [{
          value: 1,
          label: '男'
        }, {
          value: 2,
          label: '女'
        }, {
          value: 3,
          label: '未知'
        }],
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      if (this.dataForm.id) {
        this.$http({
          url: this.$http.adornUrl(`/staff/info/${this.dataForm.id}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            let staff = data.staff
            this.dataForm.username = staff.username
            this.dataForm.number = staff.number
            this.dataForm.idNumber = staff.idNumber
            this.dataForm.telnum = staff.telnum
            this.dataForm.age = staff.age
            this.dataForm.birthday = staff.birthday
            this.dataForm.sex = staff.sex
            this.dataForm.roleId = staff.roleId
          }
        })
      } else {
        if (this.$refs['dataForm'] !== undefined) {
          this.$refs['dataForm'].resetFields()
        }
        this.dataForm.username = ''
        this.dataForm.number = ''
        this.dataForm.idNumber = ''
        this.dataForm.telnum = ''
        this.dataForm.age = ''
        this.dataForm.birthday = ''
        this.dataForm.sex = ''
      }
    },
    // 选择生日后，计算年龄
    onBirthdayChange(test){
      let nowYear = new Date().getFullYear()
      if(this.dataForm.birthday){
        let year = this.dataForm.birthday.split("-")[0];
        if(year){
          if(nowYear >= year){
            this.dataForm.age = nowYear-year
          }else{
            this.dataForm.age = 0
          }
        }
      }
    },
    // 表单提交
    dataFormSubmit() {
      // return;
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/staff/save`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              username: this.dataForm.username,
              number: this.dataForm.number,
              telnum:this.dataForm.telnum,
              idNumber:this.dataForm.idNumber,
              birthday:this.dataForm.birthday,
              age:this.dataForm.age,
              sex:this.dataForm.sex,
              roleId:this.dataForm.roleId
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
  }
}
</script>

